﻿using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.MaterialManagement
{
    public class ItemManifestInfoSeachHandler : IRequestHandler<ItemManifestInfoSeachCommon, Result>
    {
        private readonly IBaseRepositoryGet<ItemManifestInfo> ItemManifestInfoRepository;
        public ItemManifestInfoSeachHandler(IBaseRepositoryGet<ItemManifestInfo> itemManifestInfoRepository)
        {
            ItemManifestInfoRepository = itemManifestInfoRepository;
        }
        public async Task<Result> Handle(ItemManifestInfoSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM ItemManifestInfo a INNER JOIN Choose c on a.ChooseId=c.ChooseId ";
            var list = ItemManifestInfoRepository.GetAll(sql).ToList();

            if (!string.IsNullOrEmpty(request.ItemManifestCode))
            {
                list = list.Where(x => x.ItemManifestCode.Contains(request.ItemManifestCode)).ToList();
            }

            if (!string.IsNullOrEmpty(request.ItemManifestVersion))
            {
                list = list.Where(x => x.ItemManifestVersion.Contains(request.ItemManifestVersion)).ToList();
            }

            
            if (request.ChooseId > 0)
            {
                list = list.Where(x => x.ChooseId == request.ChooseId).ToList();
            }

            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = list;
                result.Msg = "显示成功";
            }
            else
            {
                result.Msg = "显示失败";
            }
            return result;
        }
    }
}
